package com.openbravo.pos.sales.restaurant.kitchen;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JSplitPane;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JList;

import cn.ekuma.epos.datalogic.I_DataLogicReceipts;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.rabbit.spi.I_ModelPart;

import com.openbravo.bean.sales.KitchenWorkLine;
import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryApp;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.mode.POS_UI_Swing_Model;

import java.awt.Dimension;
import java.util.List;
import java.awt.FlowLayout;

@SuppressWarnings("serial")
public class JKitchenPanel extends JPanel implements JPanelView, BeanFactoryApp, I_ModelPart<POS_UI_Swing_Model> {
    JKitchenLines workLines, waitLines;
    private I_DataLogicReceipts dlReceipts;

    /**
     * Create the panel.
     */
    public JKitchenPanel() {
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        add(panel, BorderLayout.EAST);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JButton button = new JButton();
        button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        button.setMaximumSize(new Dimension(500, 80));
        button.setPreferredSize(new Dimension(100, 50));
        panel.add(button);

        JButton btnNewButton = new JButton("无料");
        btnNewButton.setMaximumSize(new Dimension(100, 80));
        panel.add(btnNewButton);

        JButton btnNewButton_1 = new JButton();
        btnNewButton_1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        btnNewButton_1.setMaximumSize(new Dimension(100, 80));
        panel.add(btnNewButton_1);

        JPanel panel_3 = new JPanel();
        panel.add(panel_3);
        panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));

        JList list = new JList();
        panel.add(list);

        splitPane = new JSplitPane();
        splitPane.setDividerSize(8);
        add(splitPane, BorderLayout.CENTER);

        panel_2 = new JPanel();
        splitPane.setLeftComponent(panel_2);
        panel_2.setLayout(new BorderLayout(0, 0));

        JPanel panel_1 = new JPanel();
        panel_2.add(panel_1, BorderLayout.EAST);
        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JButton button_4 = new JButton("");
        button_4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2rightarrow.png")));
        button_4.setMaximumSize(new Dimension(100, 40));
        panel_1.add(button_4);

        JButton btnNewButton_2 = new JButton("");
        btnNewButton_2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png"))); // NOI18N
        btnNewButton_2.setPreferredSize(new Dimension(57, 40));
        btnNewButton_2.setMaximumSize(new Dimension(100, 50));
        panel_1.add(btnNewButton_2);

        JButton btnNewButton_3 = new JButton("");
        btnNewButton_3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1leftarrow.png"))); // NOI18N
        btnNewButton_3.setMaximumSize(new Dimension(100, 40));
        btnNewButton_3.setPreferredSize(new Dimension(57, 40));
        panel_1.add(btnNewButton_3);

        JButton button_3 = new JButton("");
        button_3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2leftarrow.png")));
        button_3.setMaximumSize(new Dimension(100, 40));
        panel_1.add(button_3);

        JButton button_5 = new JButton("加急");
        panel_1.add(button_5);
        button_5.setMaximumSize(new Dimension(100, 80));

        JButton button_6 = new JButton("单据");
        panel_1.add(button_6);
        button_6.setMaximumSize(new Dimension(100, 80));

        JButton button_7 = new JButton("自动");
        panel_1.add(button_7);
        button_7.setMaximumSize(new Dimension(100, 80));

        workPanel = new JPanel();
        workPanel.setPreferredSize(new Dimension(10, 400));
        splitPane.setRightComponent(workPanel);
        workPanel.setLayout(new BorderLayout(0, 0));
    }

    JSplitPane splitPane;
    JPanel panel_2;
    JPanel workPanel;
    private String host;
    private I_DataLogicSystem dlSystem;

    @Override
    public String getTitle() {
        return "KitchenPanel";
    }

    @Override
    public Object getBean() {
        return this;
    }

    @Override
    public void init(I_AppView app) throws BeanFactoryException {
        host = app.getProperties().getHost();
        String lineStyle = dlSystem.getResourceAsXML("Kitchen.Lines");
        workLines = new JKitchenLines(lineStyle);
        workPanel.add(workLines, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(400, 300));
        workPanel.add(panel, BorderLayout.SOUTH);

        waitLines = new JKitchenLines(lineStyle);
        panel_2.add(waitLines, BorderLayout.CENTER);
        splitPane.setDividerLocation(0.5);
    }

    @Override
    public void activate() throws BasicException {
        reflush();

    }

    @Override
    public boolean deactivate() {

        return true;
    }

    @Override
    public JComponent getComponent() {
        return this;
    }

    public void reflush() {
        workLines.clearKitchenLines();
        waitLines.clearKitchenLines();
        try {
            List<KitchenWorkLine> workLine = dlReceipts.listKitchenWorkLineByHostCooking(host);
            List<KitchenWorkLine> waitLine = dlReceipts.listKitchenWorkLineByLineState(KitchenWorkLine.LINESTATE_wait);
            for (KitchenWorkLine l : workLine)
                workLines.addKitchenLine(l);
            for (KitchenWorkLine l : waitLine)
                waitLines.addKitchenLine(l);
        } catch (BasicException e) {

        }
    }

    @Override
    public String getModelName() {
        return POS_UI_Swing_Model.MODEL_NAME;
    }


    @Override
    public void setModel(POS_UI_Swing_Model mode) {
        this.dlReceipts = mode.getDLReceipts();
        this.dlSystem = mode.getDLSystem();

    }

}
